/*
    这是一个签到表系统
    请求/    respon返回  签到首页 one_data.html
    签到首页有form 表单提交到   /data_write 进行字符控制
    重定向到  /data_write2  渲染并显示数据


    拥有bad_ip控制，将捣乱ip禁止访问

*/

var http = require('http')
var fs = require('fs')
var url = require('url')
var comte = require('art-template')
var web_data2 = [
    {
        name:'李峰',
        bj:'玛酷中心线下助教',
        qv:'1984060072',
        ip:'127.0.0.1'
    }
]
var web_list_data = ''
var bad_ip = []

http
    .createServer(function (request,response) {
        var urlpath = url.parse(request.url,true)
        //console.log(urlpath)
        var urlpathname = urlpath.pathname
        var web_data = ''
        
        fs.readFile('./txt/bad_ip.txt',function(err,data){
            bad_ip = []
            if(err){
                console.log('找不到bad_ip文件')
            }else{
                bad_ip = data.toString().split(',\n')
            }
        })
        //var tt = require('./www/bad_ip.js')
        //bad_ip = tt.bad_ip()
        response.setHeader('Content-Type','text/html;charset=utf-8')
        console.log(bad_ip.indexOf(request.socket.remoteAddress))
        if ( bad_ip.indexOf(request.socket.remoteAddress) !== -1  ) {

            fs.readFile('./png/bad.jpg',function (error,data) {
                if(error){
                    return response.end("请叫助教老师解决")
                }else{
                    response.setHeader('Content-Type','image/jpeg;charset=utf-8')
                    response.write(data)
                    response.write("请呼叫助教老师帮助！！！！")
                    return response.end()

                }
            })

        }else if (urlpathname === '/') {
            fs.readFile('./html/one_data.html',function (err,data) {
                if (err){
                    web_data = 'not to found'
                    return response.end(web_data)
                }else {
                    web_data = data.toString()
                    return response.end(web_data)
                }
            })
        }else /*if (urlpathname.indexOf('/www/') === 0) {
            fs.readFile('.'+urlpathname,function(err,data) {
                if (err) {
                    web_data = 'not to found the urlpathname'
                    return response.end(web_data)
                }else {
                    web_data = data.toString()
                    return response.end(web_data)
                }
            })
        }else*/ if (urlpathname === '/data_write') {
            var ip_add = urlpath.query
            ip_add.ip = request.socket.remoteAddress
            web_data2.push(ip_add)
            //web_data = JSON.stringify(urlpath.query)
            response.statusCode = 302
            response.setHeader('Location','/data_write2')
            response.end()

        }else if (urlpathname === '/data_write2') {

            //return response.end(JSON.stringify(web_data2))
            fs.readFile('./html/two_data.html',function (error,data) {
                if(error){
                    response.statusCode = 302
                    response.setHeader('Location','/data_write2')
                    response.end()
                }else {

                    web_list_data = comte.render(data.toString(),{
                        web_data2: web_data2
                    })
                    return response.end(web_list_data)

                }
            })

        } else {
            response.end('请求不合法')
        }
        fs.writeFile('./txt/qiandaobiao.txt',JSON.stringify(web_data2),function (error){
            if (error){
                console.log('写入失败')
            }else{
                console.log('写入成功')
            }
        })
        /*console.log(request.socket.remoteAddress)
        console.log(bad_ip.indexOf(request.socket.remoteAddress))
        console.log(bad_ip.indexOf('/www/'))
        console.log(JSON.stringify(urlpath.query))
        console.log(request.socket.remoteAddress+request.socket.remotePort)*/
    }).listen(3000,function () {
        console.log('server is running.....')
    })